﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HaumDisplay
{
    public class HaumLine : HaumShape
    {
        /// <summary>
        /// 线起点
        /// </summary>
        public PointF startPoint;

        /// <summary>
        /// 线终点
        /// </summary>
        public PointF endPoint;

        internal Pen pen;

        /// <summary>
        /// 反色
        /// </summary>
        internal Color invertColor;

        /// <summary>
        /// 是否选中
        /// </summary>
        internal bool isSelect;

        /// <summary>
        /// 开始绘制
        /// </summary>
        internal bool startGraphic;

        public HaumLine(Color color, int borderSize = 1) {
            this.startPoint = PointF.Empty;
            this.endPoint = PointF.Empty;
            this.pen = new Pen(color, borderSize);
            //this.invertColor = Color.FromArgb(255 - color.R, 255 - color.G, 255 - color.B);
            this.invertColor = Color.FromArgb(color.R / 2, color.G / 2, color.B / 2);
            this.isSelect = false;
            this.startGraphic = false;
        }

        /// <summary>
        /// 获取图形宽高
        /// </summary>
        /// <returns></returns>
        public Size GetSize() {
            Size size = new Size((int)Math.Abs(endPoint.X - startPoint.X), (int)Math.Abs(endPoint.Y - startPoint.Y));
            return size;
        }
        
        public SizeF GetSizeF()
        {
            SizeF sizeF = new SizeF(Math.Abs(endPoint.X - startPoint.X), Math.Abs(endPoint.Y - startPoint.Y));
            return sizeF;
        }

        /// <summary>
        /// 获取距离
        /// </summary>
        /// <returns></returns>
        public float GetDistance()
        {
            float dx = endPoint.X - startPoint.X;
            float dy = endPoint.Y - startPoint.Y;
            return (float)Math.Sqrt(dx * dx + dy * dy);
        }

        /// <summary>
        /// 获取线角度
        /// </summary>
        /// <returns></returns>
        public float GetAngle()
        {
            // 计算向量的 x 和 y 分量
            float dx = endPoint.X - startPoint.X;
            float dy = endPoint.Y - startPoint.Y;
            // 使用 Math.Atan2 函数计算角度（弧度）
            double angleRad = Math.Atan2(dy, dx);
            // 将弧度转换为角度
            float angleDeg = (float)(angleRad * (180 / Math.PI));
            return angleDeg;
        }

        /// <summary>
        /// 获取线中点
        /// </summary>
        /// <returns></returns>
        public Point GetCenter()
        {
            float x = startPoint.X + (endPoint.X - startPoint.X) / 2;
            float y = startPoint.Y + (endPoint.Y - startPoint.Y) / 2;
            return new Point((int)x, (int)y);
        }

        /// <summary>
        /// 获取线中点
        /// </summary>
        /// <returns></returns>
        public PointF GetCenterF()
        {
            float x = startPoint.X + (endPoint.X - startPoint.X) / 2;
            float y = startPoint.Y + (endPoint.Y - startPoint.Y) / 2;
            return new PointF(x, y);
        }

        public override string ToString()
        {
            return $"线起始坐标P1:[{startPoint.X},{startPoint.Y}]-中心坐标:[{this.GetCenter().X},{this.GetCenter().Y}]-结束坐标P2:[{endPoint.X},{endPoint.Y}]-线段长度:[{this.GetDistance().ToString("f3")}]-角度:[{this.GetAngle().ToString("f3")}]";
        }

        public override void Graphic(Graphics g, GraphicHelper myGraphic)
        {
            g.SmoothingMode = SmoothingMode.HighQuality;

            PointF p1 = Tools.ImageTransScreenPointF(this.startPoint, myGraphic.scale, myGraphic.offset);
            PointF p2 = Tools.ImageTransScreenPointF(this.endPoint, myGraphic.scale, myGraphic.offset);
            g.DrawLine(this.pen, p1, p2);

            // 箭头长度
            float arrowLength = 10 * this.pen.Width;
            // 箭头角度（V 型开口角度）
            float arrowAngle = 30;
            // 箭头朝向角度（相对于水平向右为 0 度）
            float orientationAngle = this.GetAngle();

            // 将角度转换为弧度
            float arrowAngleRadians = arrowAngle * (float)Math.PI / 180;
            float orientationAngleRadians = orientationAngle * (float)Math.PI / 180;

            // 尖角点位置
            PointF center = Tools.ImageTransScreenPointF(this.GetCenterF(), myGraphic.scale, myGraphic.offset);
            // 计算 V 型箭头的另外两个点
            float halfAngle = arrowAngleRadians / 2;
            PointF point1 = new PointF(
                center.X - arrowLength * (float)Math.Cos(orientationAngleRadians - halfAngle),
                center.Y - arrowLength * (float)Math.Sin(orientationAngleRadians - halfAngle)
            );
            PointF point2 = new PointF(
                center.X - arrowLength * (float)Math.Cos(orientationAngleRadians + halfAngle),
                center.Y - arrowLength * (float)Math.Sin(orientationAngleRadians + halfAngle)
            );

            //绘制尖角两条线形成指向箭头
            g.DrawLine(this.pen, center, point1);
            g.DrawLine(this.pen, center, point2);

            using (Brush brush = new SolidBrush(this.isSelect ? this.invertColor : this.pen.Color))
            {
                int r = (int)this.pen.Width * 3;
                //绘制起点中点终点
                PointF start = Tools.ImageTransScreenPointF(this.startPoint, myGraphic.scale, myGraphic.offset);
                PointF end = Tools.ImageTransScreenPointF(this.endPoint, myGraphic.scale, myGraphic.offset);
                //起点
                g.FillEllipse(brush, start.X - r / 2, start.Y - r / 2, r, r);
                //中点
                //g.FillEllipse(brush, center.X - r / 2, center.Y - r / 2, r, r);
                //终点
                g.FillEllipse(brush, end.X - r / 2, end.Y - r / 2, r, r);

            }
        }
    }
}
